import 'package:deer/order/order_page.dart';
import 'package:deer/res/colors.dart';
import 'package:deer/res/font.dart';
import 'package:deer/util/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _tabIndex = 0;
  var _pageList;
  var _tabImages;
  List<String> _appBarTitles = ['订单', '商品', '统计', '店铺'];
  final _pageController = PageController();

  @override
  void initState() {
    super.initState();
    initData();
  }

  void initData() {
    _pageList = [
      Order(color: Colors.red,),
      Order(color: Colors.blue,),
      Order(color: Colors.blue,),
      Order(),
    ];
    _tabImages = [
      [
        Image.asset(Utils.getImagePath('home/icon_Order_n')),
        Image.asset(Utils.getImagePath('home/icon_Order_s'))
      ],
      [
        Image.asset(Utils.getImagePath('home/icon_commodity_n')),
        Image.asset(Utils.getImagePath('home/icon_commodity_s'))
      ],
      [
        Image.asset(Utils.getImagePath('home/icon_statistics_n')),
        Image.asset(Utils.getImagePath('home/icon_statistics_s'))
      ],
      [
        Image.asset(Utils.getImagePath('home/icon_Shop_n')),
        Image.asset(Utils.getImagePath('home/icon_Shop_s'))
      ],
    ];
  }

  Image _getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return _tabImages[curIndex][1];
    }
    return _tabImages[curIndex][0];
  }

  Widget _buildTabText(int curIndex) {
    return Padding(
      padding: const EdgeInsets.only(top: 5),
      child: Text(_appBarTitles[curIndex]),
    );
  }

  DateTime _lastTime;
  Future<bool> _isExit() {
    if (_lastTime == null || DateTime.now().difference(_lastTime) > Duration(milliseconds: 2500)) {
      _lastTime = DateTime.now();
      showToast('再次点击退出应用');
    }
    return Future.value(true);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _isExit,
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Colors.white,
          items: List.generate(_appBarTitles.length, (i) => BottomNavigationBarItem(
            icon: _getTabIcon(i),
            title: _buildTabText(i),
          )),
          currentIndex: _tabIndex,
          type: BottomNavigationBarType.fixed,
          elevation: 5,
          iconSize: 21,
          unselectedFontSize: Font.font_sp10,
          selectedFontSize: Font.font_sp10,
          selectedItemColor: Colours.app_main,
          unselectedItemColor: Color(0xffbfbfbf),
          onTap: (index) {
            _pageController.jumpToPage(index);
          },
        ),
        // 使用PageView的原因参看 https://zhuanlan.zhihu.com/p/58582876
        body: PageView(
          controller: _pageController,
          onPageChanged: _onPageChange,
          children: _pageList,
          physics: NeverScrollableScrollPhysics(), //禁止滑动
        ),
      ),
    );
  }

  void _onPageChange(int index) {
    setState(() {
      _tabIndex = index;
    });
  }
}
